package ch.il07.converter.business.impl;

import static org.junit.Assert.assertTrue;

import java.util.Iterator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.business.MeasurandAction;
import ch.il07.converter.business.UnitAction;
import ch.il07.converter.business.UserBusiness;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.utils.RandomHelper;

/**
 * Test the class UnitActionImpl
 */
public class UnitActionImplTest {

	private UnitAction ua;

	String owner = "oliver";
	UserBusiness user;
	MeasurandAction measurand;
	String testUnitPrivate = "testAddUnitPrivate";
	String testUnitPublic = "testAddUnitPublic";
	String testBaseUnit = "testBaseUnit";
	static String privateMeasurandName;
	static String publicMeasurandName;
	static {
		privateMeasurandName = RandomHelper.randString();
		publicMeasurandName = RandomHelper.randString();
	}

	/**
	 * Login the User
	 * 
	 * @throws Exception
	 *             Exception
	 */
	@Before
	public void setUp() throws Exception {
		ua = new UnitActionImpl();
		user = new UserBusinessImpl();
		measurand = new MeasurandActionImpl();
		if (user.login(owner, "123")) {
		} else {
			user.register(owner, "123", "olikoechli@gmail.com",
					AccessLevel.USER.getAccessLevelId());
		}
		measurand
				.addPrivateMeasurand(owner, privateMeasurandName, null, testBaseUnit, null);
		measurand.addPublicMeasurand(publicMeasurandName, null, testBaseUnit, null);
		System.out.println("asdf");
	}
	
	@Test
	public void testGetAllPublicUnits() {
		List<Unit> units = ua.getPublicUnitsMeasurand("Geschwindigkeit");
		assertTrue("size should be [2] but is ["+units.size()+"]", units.size() == 2);
	}
	
	@Test
	public void testGetAllPrivateUnits() {
		List<Unit> units = ua.getPrivateUnitsMeasurand("Geschwindigkeit", "hans");
		assertTrue("size should be [2] but is ["+units.size()+"]", units.size() == 2);
	}

	@Test
	public void testAddPrivateUnit() {
		assertTrue(ua.addPrivateUnit(privateMeasurandName, owner,
				testUnitPrivate, 10.0, 0.0, null));
	}

	@Test
	public void testAddPublicUnit() {
		assertTrue(ua.addPublicUnit(publicMeasurandName, testUnitPublic, 10.0,
				0.0, null));
	}

	/**/
	@Test
	public void testGetPublicUnitsMeasurand() {
		List<Unit> list = ua.getPublicUnitsMeasurand("wocjvbtgxf");
		System.out.println(list.size());
		System.out.println(list.get(0).getName());
		Iterator<Unit> i = list.iterator();
		while (i.hasNext()) {
			Unit u = i.next();
			if (u.getName().equals(publicMeasurandName)) {
				assertTrue("Owner is not the same.", u.getOwner().equals(owner));
				assertTrue("Divisor is not the same.", u.getDivisor() == 10.0);
				assertTrue("Shift is not the same.", u.getShift() == 0.0);
				break;
			}
		}
	}

	@Test
	public void testGetPrivateUnitsMeasurand() {
		List<Unit> list = ua.getPrivateUnitsMeasurand(privateMeasurandName,
				owner);
		Iterator<Unit> i = list.iterator();
		while (i.hasNext()) {
			Unit u = i.next();
			if (u.getName().equals(privateMeasurandName)) {
				assertTrue("Owner is not the same.", u.getOwner().equals(owner));
				assertTrue("Divisor is not the same.", u.getDivisor() == 10.0);
				assertTrue("Shift is not the same.", u.getShift() == 0.0);
				break;
			}
		}
	}
}
